<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\RepaymentPlanRepayment;

class RepaymentPlanRepaymentRepository
{
    /**
     * 创建用户绑定的还款计划
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return RepaymentPlanRepayment::create($params);
    }

    /**
     * 根据id获取还款计划资料
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return RepaymentPlanRepayment::find($id);
    }

    public function insertGetId($params)
    {
        return RepaymentPlanRepayment::insertGetId($params);
    }

    public function byFailMessage($repaymentPlanId)
    {
        return RepaymentPlanRepayment::where([
            ['repayment_plan_id', '=', $repaymentPlanId],
            ['fail_message', '<>', ''],
        ])->first();
    }

    public function planRepaymentToStatus($repaymentPlanId)
    {
        return RepaymentPlanRepayment::where('repayment_plan_id', $repaymentPlanId)
            ->whereIn('repayment_every_status', [3000, 3003])
            ->orderBy('repayment_every_time_date', 'asc')
            ->first();
    }

    public function repaymentSuccessSum($repaymentPlanId)
    {
        $sum = RepaymentPlanRepayment::where([
            'repayment_plan_id' => $repaymentPlanId,
            'repayment_every_status' => 3002,
        ])
            ->sum('repayment_every_time_money');
        return sprintf("%.2f", $sum);
    }

    /**
     * 根据还款计划id获取每天还款计划
     * @param $id
     * @return mixed
     */
    public function planRepaymentAll($repaymentPlanId)
    {
        return RepaymentPlanRepayment::where('repayment_plan_id', $repaymentPlanId)
            ->orderBy('repayment_every_time_date', 'asc')
            ->get();
    }

    public function byStatus($repaymentPlanId, $status)
    {
        return RepaymentPlanRepayment::where([
            'repayment_plan_id' => $repaymentPlanId,
            'repayment_every_status' => $status,
        ])
            ->first();
    }

    /**
     * 根据还款计划id和日期获取每天还款计划
     * @param $id
     * @return mixed
     */
    public function planRepaymentToDate($repaymentPlanId, $repaymentEveryTimeDate)
    {
        return RepaymentPlanRepayment::where([
            'repayment_plan_id' => $repaymentPlanId,
            'repayment_every_time_date' => $repaymentEveryTimeDate,
        ])
            ->first();
    }

    public function findByPlanRepaymentSn($planRepaymentSn)
    {
        return RepaymentPlanRepayment::where('plan_repayment_sn', $planRepaymentSn)->first();
    }

    /**
     * 更新单号
     * @param $id
     * @param $planRepaymentSn
     * @return mixed
     */
    public function updateSnById($id, $planRepaymentSn)
    {
        return RepaymentPlanRepayment::where('id', $id)->update([
            'plan_repayment_sn' => $planRepaymentSn,
        ]);
    }

    /**
     * 更新状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function updateStatusById($id, $status, $failMessage)
    {
        return RepaymentPlanRepayment::where('id', $id)->update([
            'repayment_every_status' => $status,
            'fail_message' => $failMessage,
        ]);
    }

    /**
     * 更新状态
     * @param $id
     * @param $status
     * @param $failMessage
     * @return mixed
     */
    public function updateStatusByPlanRepaymentSn($planRepaymentSn, $status, $failMessage = '')
    {
        return RepaymentPlanRepayment::where('plan_repayment_sn', $planRepaymentSn)
            ->update([
                'repayment_every_status' => $status,
                'fail_message' => $failMessage,
            ]);
    }

}